﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;
using System.IO;

namespace IntelligentFiles.HcControl
{
    public partial class boxShow : UserControl
    {
        public boxShow()
        {
            InitializeComponent();
        }
        #region <柜子相关参数>

        /// <summary>
        /// 存放所有柜子
        /// </summary>
        static Dictionary<string, cabInfo> dic_cabInfo_s = new Dictionary<string, cabInfo>();
        /// <summary>
        /// 用户模式下哪个格子可用
        /// </summary>
        public  Dictionary<string, bool> dic_box = new Dictionary<string, bool>();
        /// <summary>
        /// 每组柜子
        /// </summary>
        public class cabInfo
        {
            /// <summary>
            /// 柜子名称
            /// </summary>
            public string cabName = "";
            /// <summary>
            /// 柜子长度
            /// </summary>
            public int cab_W = 98;
            /// <summary>
            /// 柜子宽度
            /// </summary>
            public int cab_H = 68;
            /// <summary>
            /// 柜子间距
            /// </summary>
            public int interval = 5;
            /// <summary>
            /// 格子长度
            /// </summary>
            public int box_W = 98;
            /// <summary>
            /// 格子宽度
            /// </summary>
            public int box_H = 68;
            /// <summary>
            /// 格子集合
            /// </summary>
            public  List<boxInfo> boxInfo_s = new List<boxInfo>();
        }
        /// <summary>
        /// 每个格子
        /// </summary>
        public class boxInfo
        {
            /// <summary>
            /// 格子ID
            /// </summary>
            public string boxId = "";
            /// <summary>
            /// 格子序号
            /// </summary>
            public string boxName = "";
            /// <summary>
            /// 顶点坐标X
            /// </summary>
            public int X = 0;
            /// <summary>
            /// 顶点坐标Y
            /// </summary>
            public int Y = 0;
            /// <summary>
            /// 长 默认1
            /// 最小格子
            /// </summary>
            public int W = 1;
            /// <summary>
            /// 高 默认1
            /// </summary>
            public int H = 1;
        }
        #endregion

        #region 《柜子相关方法》

        /// <summary>
        /// 从配置文件读取柜子信息
        /// </summary>
        public static void readCabInfo()
        {
            //循环读取列表，直到列表为空
            string head = "Cab";
            string cabName = "A";
            int i = 1;
            int ii = 0;

            try
            {
                 string Config_File = Application.StartupPath + "/HcConfig/cabinet.ini";
                 if (File.Exists(Config_File))
                 {
                     INIClass iniClass = new INIClass(Config_File);
                     while (cabName != "")
                     {
                         cabName = iniClass.IniReadValueDefault(head + i, "cabName", "");
                         if (cabName=="")
                         {
                             return;
                         }
                         cabInfo cinfo = new cabInfo();
                         cinfo.cabName = cabName;
                         //柜子尺寸赋值
                         string cabSize = iniClass.IniReadValueDefault(head + i, "cabSize", "");
                         string[] cabSize_s = cabSize.Split(',');
                         if (cabSize_s.Length==2)
                         {
                             cinfo.cab_W=int.Parse(cabSize_s[0]);
                             cinfo.cab_H = int.Parse(cabSize_s[1]);
                         }
                         //格子尺寸赋值
                         string boxSize = iniClass.IniReadValueDefault(head + i, "boxSize", "");
                         string[] boxSize_s = boxSize.Split(',');
                         if (boxSize_s.Length == 2)
                         {
                             cinfo.box_W = int.Parse(boxSize_s[0]);
                             cinfo.box_H = int.Parse(boxSize_s[1]);
                         }
                         cinfo.interval = int.Parse(iniClass.IniReadValueDefault(head + i, "interval", ""));

                         ii = 1;
                         string box = "box";
                         while (box != "")
                         {
                             boxInfo binfo = new boxInfo();
                             //格子赋值
                             box = iniClass.IniReadValueDefault(head + i, "box"+ii, "");
                             string[] box_s = box.Split('-');

                             if (box_s.Length==6)
                             {
                                 binfo.boxId = box_s[0];
                                 binfo.boxName = box_s[1];
                                 binfo.X = int.Parse(box_s[2]);
                                 binfo.Y = int.Parse(box_s[3]);
                                 binfo.W = int.Parse(box_s[4]);
                                 binfo.H = int.Parse(box_s[5]);
                                 cinfo.boxInfo_s.Add(binfo);
                             }

                             ii++;
                         }
                         dic_cabInfo_s.Add(head + i, cinfo);
                         i++;
                     }
                 }
                 else
                 {
                     CommonApi.writeLog("柜子配置文件不存在,请检查");
                 }
            }
            catch (Exception ex)
            {
                
                CommonApi.writeLog("读取柜子参数异常,错误格子="+cabName+ii+","+ex.ToString());
            }
        }
        #endregion
        /// <summary>
        /// 显示柜子
        /// </summary>
        /// <param name="type">0 管理员登陆 全部显示 1用户登录 只显示自己可用</param>
        public void showBox(int type)
        {
            //柜子累加
            int location_X = 5;
            try
            {
                panel_back.Controls.Clear();
                foreach(var vr in dic_cabInfo_s)
                {
                    Panel pl = new Panel();
                    pl.Location=new Point(location_X,0);
                    pl.Width = vr.Value.cab_W;
                    pl.Height = vr.Value.cab_H;
                    location_X += pl.Width + vr.Value.interval;
                    for (int i = 0; i < vr.Value.boxInfo_s.Count;i++ )
                    {
                        if (vr.Value.boxInfo_s[i].boxName == "main")
                        {
                            box_main ba = new box_main();
                            ba.Width = vr.Value.box_W * vr.Value.boxInfo_s[i].W;
                            ba.Height = vr.Value.box_H * vr.Value.boxInfo_s[i].H;

                            int X = (vr.Value.box_W + vr.Value.boxInfo_s[i].W) * vr.Value.boxInfo_s[i].X;
                            int Y = (vr.Value.box_H + vr.Value.boxInfo_s[i].H) * vr.Value.boxInfo_s[i].Y;
                            ba.Location = new Point(X, Y);
                            pl.Controls.Add(ba);
                        }
                        else
                        {
                            box bx = new box();
                            bx.Width =  vr.Value.box_W * vr.Value.boxInfo_s[i].W;
                            bx.Height =  vr.Value.box_H * vr.Value.boxInfo_s[i].H;

                            int X = (vr.Value.box_W + vr.Value.boxInfo_s[i].W) * vr.Value.boxInfo_s[i].X;
                            int Y = (vr.Value.box_H + vr.Value.boxInfo_s[i].H) * vr.Value.boxInfo_s[i].Y;
                            bx.Location = new Point(X, Y);
                            if (type == 0)
                            {
                                bx.setBox(vr.Value.cabName + vr.Value.boxInfo_s[i].boxName, i, vr.Value.boxInfo_s[i].boxId,type,true);
                            }
                            else if (type == 1)
                            {
                                if (dic_box.ContainsKey(vr.Value.boxInfo_s[i].boxId))
                                {
                                    bx.setBox(vr.Value.cabName + vr.Value.boxInfo_s[i].boxName, i, vr.Value.boxInfo_s[i].boxId, type, true);
                                }
                                else
                                {
                                    bx.setBox(vr.Value.cabName + vr.Value.boxInfo_s[i].boxName, i, vr.Value.boxInfo_s[i].boxId, type, false);
                                }
                            }
                            pl.Controls.Add(bx);
                        }
                    }
                    
                    panel_back.Controls.Add(pl);
                }
            
            }
            catch(Exception ex)
            {
                CommonApi.writeLog("柜子显示异常,"+ex.ToString());
            }
            //柜子A C D
            //foreach (var var in dic_cabA)
            //{
            //    bool flag = false;
            //    if (var.Key == 1 || var.Key == 9 || var.Key == 12)
            //    {
            //        flag = true;
            //    }
            //    int X = (var.Value.X * box_X) + (var.Value.X * 2);
            //    int Y = (var.Value.Y * box_Y) + (var.Value.Y * 2);
            //    int W = var.Value.W * box_X + 2 * (var.Value.W - 1);
            //    int H = var.Value.H * box_Y + 2 * (var.Value.H - 1);


            //    Admin bx = new Admin();
            //    bx.Width = W;
            //    bx.Height = H;
            //    bx.Location = new Point(X, Y);

            //    bx.setBoxAdmin(var.Value.boxName, var.Key, flag);
            //    panel_1.Controls.Add(bx);
            //}
           
          
        }
       

        private void boxShow_Load(object sender, EventArgs e)
        {
            
        }
    }
}
